﻿using Cache.Redis;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// DataBusiness 的摘要说明
/// </summary>
public class DataBusiness
{
    /// 保存数据
    /// <summary>
    /// 保存数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="t"></param>
    public static bool SaveObject<T>(T t) where T : Model.Public.BaseClass<T>, new()
    {
        bool result = false;
        if (t.GetKey() > 0)
        {
            //update mysql
            result = t.UpdateData();
            if (result)
            {
                //update redis
                RedisHelper.instance.RemoveObject(t.GetDBName(), t.GetKey().ToString());
            }
        }
        else
        {
            //insert mysql
            int id = t.InsertData();
            if (id > 0)
            {
                result = true;
                t.SetKey(id);
                //插入成功，向Redis插入数据
                RedisHelper.instance.SaveHashObject<T>(t);
                RedisHelper.instance.ExpireEntry(t.GetDBName(), new TimeSpan(1, 0, 0));
            }
        }
        return result;
    }


    //public static bool UpdateObject<T>(T t) where T : Model.Public.BaseClass<T>, new()
    //{
    //    bool result = t.UpdateData();
    //    if (result)
    //    {
    //        //update redis
    //        RedisHelper.instance.RemoveObject(t.GetDBName(), t.GetKey().ToString());
    //    }
    //    return result;
    //}


    //public static bool InsertObject<T>(T t) where T : Model.Public.BaseClass<T>, new()
    //{
    //    bool result = false;
    //    int id = t.InsertData();
    //    if (id > 0)
    //    {
    //        result = true;
    //        t.SetKey(id);
    //    }
    //    return result;
    //}

    //public static void UpdateObjectList<T>(List<T> list) where T : Model.Public.BaseClass<T>, new()
    //{
    //    if (list != null && list.Any())
    //    {
    //        foreach (var item in list)
    //        {
    //            UpdateObject<T>(item);
    //        }
    //    }
    //}
    //public static void InsertObjectList<T>(List<T> list) where T : Model.Public.BaseClass<T>, new()
    //{
    //    if (list != null && list.Any())
    //    {
    //        foreach (var item in list)
    //        {
    //            InsertObject<T>(item);
    //        }
    //    }
    //}


    /// 批量插入数据
    /// <summary>
    /// 批量插入数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="list"></param>
    public static void SaveObjectList<T>(List<T> list) where T : Model.Public.BaseClass<T>, new()
    {
        if (list != null && list.Any())
        {
            foreach (var item in list)
            {
                SaveObject<T>(item);
            }
        }
    }

    /// 获取单个数据
    /// <summary>
    /// 获取单个数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="uid"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static T GetObject<T>(string uid, int id) where T : class, Model.Public.BaseClass<T>, new()
    {
        T t = RedisHelper.instance.GetHashObject<T>(uid, id.ToString());
        if (t == null || t.GetKey() <= 0)
        {
            t = new T();
            t.SetDBName(uid);
            t.SetKey(id);
            Dictionary<string, object> p = new Dictionary<string, object>();
            p.Add("UID", uid);
            p.Add("Id", id);
            if (t.SelectData(p))
            {
                //获取成功，向Redis插入数据
                RedisHelper.instance.SaveHashObject<T>(t);
            }
            else
            {
                t = null;
            }
        }
        return t;
    }

    /// 获取数据列表
    /// <summary>
    /// 获取数据列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="uid"></param>
    /// <returns></returns>
    public static List<T> GetObjectList<T>(string uid) where T : Model.Public.BaseClass<T>, new()
    {
        T t = new T();
        List<T> list = RedisHelper.instance.GetHashObjectList<T>(uid);
        Dictionary<string, object> p = new Dictionary<string, object>();
        p.Add("UID", uid);
        if (list == null || !list.Any())
        {
            list = t.GetDataList(p);
            if (list != null && list.Any())
            {
                t.SetDBName(uid);
                RedisHelper.instance.SaveRangeObjectInHash<T>(uid, list);
                RedisHelper.instance.ExpireEntry(t.GetDBName(), new TimeSpan(1, 0, 0));
            }
        }
        else
        {
            //获取同个UID下的数据个数
            int count = t.GetCount(p);
            if (count != list.Count)
            {
                list = t.GetDataList(p);
                if (list != null && list.Any())
                {
                    t.SetDBName(uid);
                    RedisHelper.instance.RemoveKey<T>(uid);
                    RedisHelper.instance.SaveRangeObjectInHash<T>(uid, list);
                    RedisHelper.instance.ExpireEntry(t.GetDBName(), new TimeSpan(1, 0, 0));
                }
            }
        }
        return list;
    }

    /// 删除数据
    /// <summary>
    /// 删除数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="uid"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static bool RemoveObject<T>(string uid, int id) where T : Model.Public.BaseClass<T>, new()
    {
        bool result = false;
        T t = new T();
        Dictionary<string, object> p = new Dictionary<string, object>();
        p.Add("UID", uid);
        p.Add("Id", id.ToString());
        if (t.DeleteData(p))
        {
            result = true;
            RedisHelper.instance.RemoveObject<T>(uid, id.ToString());
        }
        return result;
    }

    /// 获取数据 针对Role表这种，以uid为key的Redis结构
    /// <summary>
    /// 获取数据 针对Role表这种，以uid为key的Redis结构
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="uid"></param>
    /// <returns></returns>
    public static T GetValue<T>(string uid) where T : class, Model.Public.BaseClass<T>, new()
    {
        T t = RedisHelper.instance.GetHashObject<T>(uid, uid);
        if (t == null || t.GetKey() <= 0)
        {
            t = new T();
            t.SetDBName(uid);
            t.SetKey(Convert.ToInt32(uid));
            Dictionary<string, object> p = new Dictionary<string, object>();
            p.Add("UID", uid);
            if (t.SelectData(p))
            {
                RedisHelper.instance.SaveHashValue<T>(t, uid);
            }
            else
            {
                t = null;
            }
        }
        return t;
    }

    /// 获取同一个账号下的数据个数
    /// <summary>
    /// 获取同一个账号下的数据个数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="uid"></param>
    /// <returns></returns>
    public static int GetCount<T>(string uid) where T : class, Model.Public.BaseClass<T>, new()
    {
        T t = new T();
        Dictionary<string, object> p = new Dictionary<string, object>();
        p.Add("UID", uid);
        int result = t.GetCount(p);
        return result;
    }
}
